<template>
  <div>
    <el-container>
      <el-header class="homeHeader">
        <div class="title">云E办</div>
        <div>
          <el-button icon="el-icon-bell" type="text" style="color: black;margin-right: 8px" size="normal" @click="goToFrienfChat()"></el-button>
          <el-dropdown :hide-on-click="false" class="userInfo" @command="handleCommand">
          <span class="el-dropdown-link" >
            {{user.name}}<i><img :src="user.userFace"></i>
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command = "userInfo">个人中心</el-dropdown-item>
            <el-dropdown-item command = "setting">设置</el-dropdown-item>
            <el-dropdown-item command = "logout">注销登录</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
        </div>
      </el-header>
      <el-container >
        <el-aside width="200px" style="overflow: hidden">
          <el-menu router unique-opened>
            <el-submenu :index="index+''" v-for="(items,index) in routes" :key="index" v-show="!items.hidden">
              <template slot="title">
                <i :class="items.iconCls" style="color: #1accff;margin-right:5px"></i>
                <span slot="title" style="font-size: 16px">{{items.name}}</span>
              </template>
              <el-menu-item :index="item.path" v-for="(item,indexs) in items.children" :key="indexs">{{item.name}}</el-menu-item>
              
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main>
          <el-breadcrumb separator-class="el-icon-arrow-right" v-if="this.$router.currentRoute.path != '/home'">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>{{this.$router.currentRoute.name}}</el-breadcrumb-item>
            
          </el-breadcrumb>
          <div class="homeWelcome" v-show="this.$router.currentRoute.path == '/home'">
              欢迎来到云E办系统!
            </div>
          <router-view class="routeActiveName"/>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  name: "Home",
  data(){
    return{
      // user: JSON.parse(window.sessionStorage.getItem('user')),
    }
  },
  computed: {
    routes() {
      return this.$store.state.routes;
    },
    user() {
      if(!this.$store.state.currentAdmin){
        this.$store.commit('INIT_CURRENTAdmin',JSON.parse(window.sessionStorage.getItem('user')));
      }
      return this.$store.state.currentAdmin;
    }
  },
  methods: {
    goToFrienfChat(){
      this.$router.push('/friendChat');
    },
    handleCommand(command) {
      if(command == "logout"){
        this.$confirm('确定要退出登录吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$post('/logout');
          // 清除用户信息和token
          window.sessionStorage.removeItem('tokenStr');
          window.sessionStorage.removeItem('user');
          //清除菜单
          this.$store.commit('setRoutes',[]);
          //跳转登录页
          this.$router.replace('/');

        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });          
        });
      } else if(command == "userInfo"){
        this.$router.push('/adminInfo');
      }
    }
        
      
  }
};
</script>


<style scoped>
 .homeHeader {
   background: #409eff;
   display: flex;   
   align-content: center;
   justify-content: space-between;
   padding: 0 15px;
   box-sizing: border-box;
 }
 .homeHeader .title {
   font-family: 楷体;
   font-size: 30px;
   color: white;
   margin-top: 10px; 
 }

 .homeHeader .userInfo{
   font-size: 16px;
   cursor: pointer;
 }
 .el-dropdown-link img{
   width: 48px;
   height: 48px;
   border-radius: 50%;
   margin-left: 8px;
   margin-top: 5px;
 }
 .homeWelcome{
   text-align: center;
   font-size: 30px;
   font-family: 楷体;
   color: #409eff;
   padding-top: 50px;
 }
 .routeActiveName{
   margin-top: 10px;
 }
</style>